package com.lemon.exam.common.crud;

import com.lemon.exam.common.annotation.NestedParam;
import com.lemon.exam.common.web.Param;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * 基本控制器，主要配置基本的增删改查，用来被继承
 *
 * @author Lemon
 * @since 2025/3/24 11:47
 */
public abstract class BaseController<T, ID, S extends BaseService<T, ID>> {
    @Autowired(required = false)
    protected S service;

    /**
     * 插入
     *
     * @param param
     * @return
     */
    @PostMapping("insert")
    @Operation(summary = "插入")
    protected Mono<String> insert(@Valid @RequestBody T param) {
        return service.insert(param);
    }

    /**
     * 更新
     *
     * @param param
     * @return
     */
    @PutMapping("update")
    @Operation(summary = "更新")
    protected Mono<String> update(@Valid @RequestBody T param) {
        return service.update(param);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("deleteById/{id}")
    @Operation(summary = "删除", description = "根据主键删除")
    protected Mono<String> deleteById(@PathVariable("id") ID id) {
        return service.deleteById(id);
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @GetMapping("findById/{id}")
    @Operation(summary = "查询", description = "根据主键查询")
    protected Mono<?> findById(@PathVariable("id") ID id) {
        return service.findById(id);
    }

    /**
     * 存在
     *
     * @param id
     * @return
     */
    @GetMapping("existsById/{id}")
    @Operation(summary = "存在", description = "根据主键判断")
    protected Mono<Boolean> existsById(@PathVariable("id") ID id) {
        return service.existsById(id);
    }

    /**
     * 查询
     *
     * @param ids
     * @return
     */
    @GetMapping("findAllById")
    @Operation(summary = "查询", description = "根据主键集合查询")
    protected Flux<?> findAllById(@RequestParam Iterable<ID> ids) {
        return service.findAllById(ids);
    }

    /**
     * 条数
     *
     * @param param
     * @return
     */
    @GetMapping("count")
    @Operation(summary = "条数")
    protected Mono<Long> count(@NestedParam Map<String, Object> param) {
        return service.count(param);
    }

    /**
     * 查询
     *
     * @param param
     * @return
     */
    @GetMapping("find")
    @Operation(summary = "查询", description = "条件查询")
    protected Flux<?> find(@NestedParam Map<String, Object> param) {
        return service.find(param);
    }

    /**
     * 分页
     *
     * @param param
     * @return
     */
    @GetMapping("page")
    @Operation(summary = "分页")
    protected Mono<Page<?>> page(@NestedParam Param param) {
        return service.page(param);
    }

    /**
     * 启用
     *
     * @param id
     * @return
     */
    @PutMapping("enable/{id}")
    @Operation(summary = "启用")
    protected Mono<String> enable(@PathVariable("id") ID id) {
        return service.enable(id);
    }

    /**
     * 停用
     *
     * @param id
     * @return
     */
    @PutMapping("unable/{id}")
    @Operation(summary = "停用")
    protected Mono<String> unable(@PathVariable("id") ID id) {
        return service.unable(id);
    }
}
